﻿using FineUI;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Web;
using System.Web.Services;
using ZHDJ.BranchCenter.BLL;
using ZHDJ.Data.Models.Center;
using ZHDJ.WebPage;

namespace ZHDJ.BranchCenter.Module.OrganizationalLife
{
    public partial class MemberCostList : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "PartyMemberCostView";
            }
        }


        public string Category
        {
            get
            {
                return Request.QueryString["Category"] ?? "BranchHonour";
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        #region 数据加载
        private void BindData()
        {
            var year = DateTime.Now.Year;
            for (int i = year - 5; i <= year + 5; i++)
            {
                this.ddlYear.Items.Add(new ListItem() { Text = i.ToString(), Value = i.ToString() });
            }
            this.ddlYear.SelectedValue = year.ToString();

            // 每页记录数
            Grid1.PageSize = ConfigHelper.PageSize;
            ddlGridPageSize.SelectedValue = ConfigHelper.PageSize.ToString();

            GridTotal.PageSize = ConfigHelper.PageSize;
            ddlGridPageSizeTotal.SelectedValue = ConfigHelper.PageSize.ToString();

            // 权限检查
            //CheckPowerWithButton("PartyMemberCostAdd", btnNew);
            //btnNew.OnClientClick = Window1.GetShowReference("PartyMemberCostEdit.aspx?Category=" + Category, "新增");
            this.ppBranch.WindinsUrl = "../../../Share/Picker/BranchSinglePicker.aspx?BranchID=" + BranchID;
            this.ppBranch.Value = BranchID;
            this.ppBranch.Text = ControlBusiness.GetBranchPickerText(BranchID);
            this.ppBranch.ShowTrigger1 = false;
        }
        private void LoadData()
        {
            var branch = DB.PartyOrgBranchs.Find(this.ppBranch.Value);
            if (!(new List<string>() { "3", "5" }).Contains(branch.BranchMark))
            {
                //统计页面
                var ds = PartyMemberCost.GetBranchCostTotal(GridTotal.PageIndex, GridTotal.PageSize, this.ppBranch.Value,
               Convert.ToInt32(ddlYear.SelectedValue), "");

                GridTotal.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
                GridTotal.DataSource = ds.Tables[0];
                GridTotal.DataBind();
                Grid1.Hidden = true;
                GridTotal.Hidden = false;
                txtUserName.Hidden = true;
            }
            else
            {
                //缴费
                var ds = PartyMemberCost.GetBranchCost(Grid1.PageIndex, Grid1.PageSize, this.ppBranch.Value,
                    Convert.ToInt32(ddlYear.SelectedValue),
                    txtUserName.Text);

                Grid1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
                Grid1.DataSource = ds.Tables[0];
                Grid1.DataBind();
                Grid1.Hidden = false;
                GridTotal.Hidden = true;
                txtUserName.Hidden = false;
            }
        }
        #endregion

        #region 按钮事件
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            GridTotal.PageIndex = e.NewPageIndex;
            LoadData();
        }
        protected void GridTotal_PageIndexChange(object sender, GridPageEventArgs e)
        {
            GridTotal.PageIndex = e.NewPageIndex;
            LoadData();
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        protected void ppBranch_TextChanged(object sender, EventArgs e)
        {
            ppBranch.ShowTrigger1 = !string.IsNullOrEmpty(ppBranch.Text);
        }

        protected void ddlGridPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlGridPageSize.SelectedValue);
            LoadData();
        }

        protected void ddlGridPageSizeTotal_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridTotal.PageSize = Convert.ToInt32(ddlGridPageSizeTotal.SelectedValue);
            LoadData();
        }
        protected void btnExport_Click(object sender, EventArgs e)
        {
            var ds = PartyMemberCost.GetBranchCost(0, Grid1.RecordCount, this.ppBranch.Value,
                    Convert.ToInt32(ddlYear.SelectedValue),
                    txtUserName.Text);

            //var dt = ds.Tables[0];
            #region 表格格式化
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("姓名"));
            dt.Columns.Add(new DataColumn("组织名称"));
            dt.Columns.Add(new DataColumn("党费基数"));
            dt.Columns.Add(new DataColumn("1月"));
            dt.Columns.Add(new DataColumn("2月"));
            dt.Columns.Add(new DataColumn("3月"));
            dt.Columns.Add(new DataColumn("4月"));
            dt.Columns.Add(new DataColumn("5月"));
            dt.Columns.Add(new DataColumn("6月"));
            dt.Columns.Add(new DataColumn("7月"));
            dt.Columns.Add(new DataColumn("8月"));
            dt.Columns.Add(new DataColumn("9月"));
            dt.Columns.Add(new DataColumn("10月"));
            dt.Columns.Add(new DataColumn("11月"));
            dt.Columns.Add(new DataColumn("12月"));
            dt.Columns.Add(new DataColumn("汇总"));

            foreach (DataRow row in ds.Tables[0].Rows)
            {
                var newRow = dt.NewRow();
                newRow[0] = row[1];
                newRow[1] = row[3];
                newRow[2] = row[5];
                newRow[3] = row[11];
                newRow[4] = row[16];
                newRow[5] = row[21];
                newRow[6] = row[26];
                newRow[7] = row[31];
                newRow[8] = row[36];
                newRow[9] = row[41];
                newRow[10] = row[46];
                newRow[11] = row[51];
                newRow[12] = row[56];
                newRow[13] = row[61];
                newRow[14] = row[66];
                newRow[15] = row[67];

                dt.Rows.Add(newRow);
            }
            #endregion

            string filePath = Server.MapPath("~/upload/" + this.ppBranch.Text + "党员缴费_" + DateTime.Now.ToString("yyyy-MM-dd") + ".xls");
            string sheetName = "党员缴费";

            SaveFile(filePath, sheetName, dt);
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            var ds = PartyMemberCost.GetBranchCostTotal(0, GridTotal.RecordCount, this.ppBranch.Value,
              Convert.ToInt32(ddlYear.SelectedValue), "");

            var dt = ds.Tables[0];
            #region 列名中文化
            dt.Columns[1].ColumnName = "组织名称";
            dt.Columns[2].ColumnName = "党员总数";
            dt.Columns[3].ColumnName = "1月人数";
            dt.Columns[4].ColumnName = "1月缴费";
            dt.Columns[5].ColumnName = "2月人数";
            dt.Columns[6].ColumnName = "2月缴费";
            dt.Columns[7].ColumnName = "3月人数";
            dt.Columns[8].ColumnName = "3月缴费";
            dt.Columns[9].ColumnName = "4月人数";
            dt.Columns[10].ColumnName = "4月缴费";
            dt.Columns[11].ColumnName = "5月人数";
            dt.Columns[12].ColumnName = "5月缴费";
            dt.Columns[13].ColumnName = "6月人数";
            dt.Columns[14].ColumnName = "6月缴费";
            dt.Columns[15].ColumnName = "7月人数";
            dt.Columns[16].ColumnName = "7月缴费";
            dt.Columns[17].ColumnName = "8月人数";
            dt.Columns[18].ColumnName = "8月缴费";
            dt.Columns[19].ColumnName = "9月人数";
            dt.Columns[20].ColumnName = "9月缴费";
            dt.Columns[21].ColumnName = "10月人数";
            dt.Columns[22].ColumnName = "10月缴费";
            dt.Columns[23].ColumnName = "11月人数";
            dt.Columns[24].ColumnName = "11月缴费";
            dt.Columns[25].ColumnName = "12月人数";
            dt.Columns[26].ColumnName = "12月缴费";
            #endregion
            dt.Columns.RemoveAt(27);
            dt.Columns.RemoveAt(0);

            string filePath = Server.MapPath("~/upload/党员缴费统计_" + DateTime.Now.ToString("yyyy-MM-dd") + ".xls");
            string sheetName = "党员缴费统计";

            SaveFile(filePath, sheetName, dt);
        }
        private void SaveFile(string filePath, string sheetName, DataTable dt)
        {
            try
            {
                NPOIBusiness.ExportToSave(filePath, sheetName, dt);

                string tmpRootDir = Server.MapPath(HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录
                filePath = filePath.Replace(tmpRootDir, "");
                filePath = filePath.Replace(@"\", @"/");
                filePath = HttpContext.Current.Request.Url.Host + ":" + HttpContext.Current.Request.Url.Port + "/" + filePath;
                string script = "window.open('http://" + filePath + "');";
                PageContext.RegisterStartupScript(script);
            }
            catch (Exception)
            {
                PageContext.RegisterStartupScript(FineUI.Alert.GetShowReference("导出失败！", "错误"));
            }
        }

        #endregion

        protected void Dialog_Close(object sender, EventArgs e)
        {
            LoadData();
        }

        protected void Window1_Close(object sender, WindowCloseEventArgs e)
        {
            LoadData();
        }
    }
}